//
//  HWTimeChooseView.m
//  嗨文
//
//  Created by 响当当 on 2019/2/15.
//  Copyright © 2019年 dongzheng. All rights reserved.
//

#import "HWTimeChooseView.h"

@interface HWTimeChooseView()<UIPickerViewDelegate,UIPickerViewDataSource>
{
    NSInteger _yearIndex;
    
    NSInteger _monthIndex;
}
@property (nonatomic, strong) NSMutableArray *yearArray;

@property (nonatomic, strong) NSMutableArray *monthArray;

@property (nonatomic, strong)UIPickerView *timePickeView;//时间选择
@property (nonatomic, strong)UIView *topView;//时间选择
@end

@implementation HWTimeChooseView

- (NSMutableArray *)yearArray {
    
    if (_yearArray == nil) {
        
        _yearArray = [NSMutableArray array];
        
        for (int year = 2000; year < 2050; year++) {
            
            NSString *str = [NSString stringWithFormat:@"%d年", year];
            
            [_yearArray addObject:str];
        }
    }
    
    return _yearArray;
}

- (NSMutableArray *)monthArray {
    
    if (_monthArray == nil) {
        
        _monthArray = [NSMutableArray array];
        
        for (int month = 1; month <= 12; month++) {
            
            NSString *str = [NSString stringWithFormat:@"%02d月", month];
            
            [_monthArray addObject:str];
        }
    }
    
    return _monthArray;
}

- (UIPickerView *)timePickeView
{
    if (!_timePickeView)
    {
        _timePickeView = [[UIPickerView alloc]initWithFrame:CGRectMake(0,SCREEN_HEIGHT-300*kUIScaleBasePlus,SCREEN_WIDTH, 300*kUIScaleBasePlus)];
        _timePickeView.delegate = self;
        _timePickeView.dataSource =self;
        _timePickeView.backgroundColor = [UIColor whiteColor];
    }
    return _timePickeView;
}

- (UIView *)topView
{
    if (!_topView)
    {
        _topView = [[UIView alloc] initWithFrame:CGRectMake(0,SCREEN_HEIGHT-300*kUIScaleBasePlus-40*kUIScaleBasePlus, SCREEN_WIDTH, 40*kUIScaleBasePlus)];
        _topView.backgroundColor = RGBACOLOR(242, 242, 242, 1.0);
        
        UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        cancelBtn.frame = CGRectMake(0, 0,100*kUIScaleBasePlus,40*kUIScaleBasePlus);
        [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
        [cancelBtn setTitleColor:LCHexColor(0x000000) forState:UIControlStateNormal];
        [cancelBtn.titleLabel setFont:[UIFont systemFontOfSize:16*kUIScaleBasePlus]];
        [cancelBtn addTarget:self action:@selector(cancelClick) forControlEvents:UIControlEventTouchUpInside];
        [_topView addSubview:cancelBtn];
        
        UIButton *yesBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        yesBtn.frame = CGRectMake(SCREEN_WIDTH - 100*kUIScaleBasePlus, 0, 100*kUIScaleBasePlus,40*kUIScaleBasePlus);
        [yesBtn setTitle:@"完成" forState:UIControlStateNormal];
        [yesBtn setTitleColor:LCHexColor(0xF54337) forState:UIControlStateNormal];
        [yesBtn.titleLabel setFont:[UIFont systemFontOfSize:16*kUIScaleBasePlus]];
        [yesBtn addTarget:self action:@selector(yesBtnClick) forControlEvents:UIControlEventTouchUpInside];
        [_topView addSubview:yesBtn];
    }
    return _topView;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.75];
        [self addSubview:self.topView];
        [self addSubview:self.timePickeView];
        [self  initialize];
    }
    return self;
}

#pragma mark - 初始化
- (void)initialize
{
    NSCalendar *calendar = [[NSCalendar alloc]
                            initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    // 定义一个时间字段的旗标，指定将会获取指定年、月、日、时、分、秒的信息
    unsigned unitFlags = NSCalendarUnitYear |
    NSCalendarUnitMonth |  NSCalendarUnitDay |
    NSCalendarUnitHour |  NSCalendarUnitMinute |
    NSCalendarUnitSecond | NSCalendarUnitWeekday;
    // 获取不同时间字段的信息
    NSDateComponents *comp = [calendar components: unitFlags fromDate:[NSDate date]];
    
    _yearIndex = [self.yearArray indexOfObject:[NSString stringWithFormat:@"%ld年", comp.year]];
    _monthIndex = [self.monthArray indexOfObject:[NSString stringWithFormat:@"%02ld月", comp.month]];
 
    
    [self.timePickeView selectRow:_yearIndex inComponent:0 animated:YES];
    [self.timePickeView  selectRow:_monthIndex inComponent:1 animated:YES];
    
    [self pickerView:self.timePickeView  didSelectRow:_yearIndex inComponent:0];
    [self pickerView:self.timePickeView  didSelectRow:_monthIndex inComponent:1];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        UILabel *label = (UILabel *)[self.timePickeView viewForRow:_yearIndex forComponent:0];
        label.textColor = RGBACOLOR(26, 174, 135, 1.0);
        label.font = [UIFont systemFontOfSize:16*kUIScaleBasePlus];
        
        label = (UILabel *)[self.timePickeView viewForRow:_monthIndex forComponent:1];
        label.textColor = RGBACOLOR(26, 174, 135, 1.0);
        label.font = [UIFont systemFontOfSize:16*kUIScaleBasePlus];
        
    });
    
    [UIView animateWithDuration:0.25 animations:^{
        
        self.topView.frame = CGRectMake(0,SCREEN_HEIGHT-300*kUIScaleBasePlus-40*kUIScaleBasePlus, SCREEN_WIDTH, 40*kUIScaleBasePlus);
        self.timePickeView.frame = CGRectMake(0,SCREEN_HEIGHT-300*kUIScaleBasePlus,SCREEN_WIDTH, 300*kUIScaleBasePlus);
    }];
}

- (void)remove {
    
    [UIView animateWithDuration:0.25 animations:^{
        
        self.topView.frame = CGRectMake(0,SCREEN_HEIGHT-300*kUIScaleBasePlus-40*kUIScaleBasePlus, SCREEN_WIDTH, 40*kUIScaleBasePlus);
        self.timePickeView.frame = CGRectMake(0,SCREEN_HEIGHT-300*kUIScaleBasePlus,SCREEN_WIDTH, 300*kUIScaleBasePlus);
        
    } completion:^(BOOL finished) {
        
        [self removeFromSuperview];
    }];
    
}

#pragma mark - delegate & datasource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 2;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if (component == 0)
    {
        return self.yearArray.count;
    }
    else
    {
        return self.monthArray.count;
    }
}

#pragma mark -UIPickerView的代理

// 滚动UIPickerView就会调用
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if (component == 0) {
        
        _yearIndex = row;
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            UILabel *label = (UILabel *)[pickerView viewForRow:row forComponent:component];
            label.textColor = RGBACOLOR(26, 174, 135, 1.0);
            label.font = [UIFont systemFontOfSize:16*kUIScaleBasePlus];
            
        });
        
    }else{
        
        _monthIndex = row;
        
        [pickerView reloadComponent:1];
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            UILabel *label = (UILabel *)[pickerView viewForRow:row forComponent:component];
            label.textColor = RGBACOLOR(26, 174, 135, 1.0);
            label.font = [UIFont systemFontOfSize:16*kUIScaleBasePlus];
            
        });
    }
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    //    //设置分割线的颜色
    //    for(UIView *singleLine in pickerView.subviews)
    //    {
    //        if (singleLine.frame.size.height < 1)
    //        {
    //            singleLine.backgroundColor = kSingleLineColor;
    //        }
    //    }
    
    //设置文字的属性
    UILabel *genderLabel = [[UILabel alloc] init];
    genderLabel.textAlignment = NSTextAlignmentCenter;
    genderLabel.textColor = RGBACOLOR(153, 153, 153, 1.0);
    genderLabel.font = [UIFont systemFontOfSize:14*kUIScaleBasePlus];
    if (component == 0) {
        
        genderLabel.text = self.yearArray[row];
        
    }else if (component == 1) {
        
        genderLabel.text = self.monthArray[row];
        
    }
    
    return genderLabel;
}


#pragma mark - Method
- (void)cancelClick
{
    [self remove];
}

- (void)yesBtnClick
{
    if (self.getDateBlock)
    {
        NSString *timeStr = [NSString stringWithFormat:@"%@%@",((UILabel *)[self.timePickeView viewForRow:_yearIndex forComponent:0]).text, ((UILabel *)[self.timePickeView viewForRow:_monthIndex forComponent:1]).text];
        self.getDateBlock(timeStr);
        [self remove];
        }
}
@end
